﻿using MyX.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace LearningEnglish.Models
{
    public class FileService : XEntity
    {
        public void AddLocalFile(string filePath)
        {
            var info = new FileInfo(filePath);
            var name = info.Name;
            var virtualPath = info.FullName.Substring(2).Replace("\\", "/");
            var serverPath = info.FullName;
            Add(name, virtualPath, serverPath);
        }
        public void Add(string fileName, string virtualPath, string serverPath)
        {
            if (Has(fileName))
                throw new Exception("已存在该文件名");

            Update(new FileAdded()
            {
                FileID = Guid.NewGuid(),
                FileName = fileName,
                VirtualPath = virtualPath,
                ServerPath = serverPath
            });
        }

        public bool Has(string fileName)
        {
            return _files.Exists(x => x.FileName == fileName);
        }

        public Guid GetFileID(string fileName)
        {
            return (from p in _files where p.FileName == fileName select p.FileID).Single();
        }

        public string GetVirtualPath(Guid fileID)
        {
            return (from p in _files where p.FileID == fileID select p.VirtualPath).Single();
        }

        public string GetServerPath(Guid fileID)
        {
            return (from p in _files where p.FileID == fileID select p.ServerPath).Single();
        }

        public void Do(FileAdded e)
        {
            _files.Add(new FileServiceFileInfo()
            {
                FileID = e.FileID,
                FileName = e.FileName,
                VirtualPath = e.VirtualPath,
                ServerPath = e.ServerPath
            });
        }

        private FileService()
            : base()
        {
            _files = new List<FileServiceFileInfo>();
        }

        private List<FileServiceFileInfo> _files;

        static FileService()
        {
            _serviceID = Guid.Parse("EFE03B14-02DD-4B21-9564-70B469752D09");
            _instance = new FileService();
            _instance.ID = _serviceID;
            _instance.Load();
        }

        private static Guid _serviceID;

        public static FileService Instance
        {
            get
            {
                if (_instance == null)
                    throw new Exception("实例化未知错误");
                return _instance;
            }
        }
        private static FileService _instance;
    }

    public class FileServiceFileInfo
    {
        public Guid FileID { get; set; }

        public string FileName { get; set; }

        /// <summary>
        /// 文件的虚拟路径
        /// </summary>
        public string VirtualPath { get; set; }

        /// <summary>
        /// 文件在服务器上的存储路径
        /// </summary>
        public string ServerPath { get; set; }

    }

    public class FileAdded : XEvent
    {
        public Guid FileID { get; set; }

        public string FileName { get; set; }

        /// <summary>
        /// 虚拟路径
        /// <para>该路径作为向客户端提供的文件路径</para>
        /// </summary>
        public string VirtualPath { get; set; }

        /// <summary>
        /// 服务器路径
        /// <para>在服务器上存储的位置，用在移动，删除等文件操作中</para>
        /// </summary>
        public string ServerPath { get; set; }
    }
}